package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import DataObjects.ConstantObjects;
import DataObjects.Supplier;
import Messages.MessageAddNewSupplier;
import Messages.MessageSavingReply;
import Messages.MessageShowEmployeeIDReply;


public class PfAdd_new_supplier extends JPanel {
	
	private MainMenu mainMenu;
	private String supID;
	
	private JPanel infoPanel;
	private JPanel buttonsPanel;
	private JPanel check_panel;
	private JPanel check_buttons_panel;
	private JPanel addItemsButtonPanel;
	private JPanel saveClearButtonPanel;
	
	private VKTextField textField_name;
	private VKTextField textField_city;
	private VKTextField textField_street;
	private VKTextField textField_house;
	private VKTextField textField_zipcode;
	private VKTextField textField_phoneNo1;	
	private VKTextField textField_phoneNo2;
	private VKTextField textField_phoneCell;
	private VKTextField textField_email;
	private VKTextField textField_fax;
	private VKTextField textField_contactMen;
	private VKTextField textField_id;
	
	private JButton btnItems;
	
	private JComboBox statusCB;

	private int sId;
	private int isSaved=0;
	
	public PfAdd_new_supplier (MainMenu mm,String suppID)
	{
		super();
		this.setLayout(new BorderLayout(0, 0));
		this.mainMenu=mm;
		this.supID=suppID;		
		sId=Integer.parseInt(supID);		
		sId=sId+1;	
		initialize();		
	}
	
	
	
	
	private void initialize()
	{
		this.add(getInfoPanel(),BorderLayout.CENTER);
		this.add(get_check_buttons_panel(),BorderLayout.EAST);
	}
	
	
	
	private JPanel getInfoPanel()
	{
		infoPanel=new JPanel();
		infoPanel.setBackground(new Color(255, 250, 205));
		infoPanel.setForeground(new Color(0, 0, 0));
		infoPanel.setBorder(new TitledBorder(null, "\u05E4\u05E8\u05D8\u05D9 \u05E1\u05E4\u05E7", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		infoPanel.setLayout(new SpringLayout());
		
		textField_id = new VKTextField(mainMenu);
		textField_id.setHorizontalAlignment(JTextField.RIGHT);
		textField_id.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_id.setText(Integer.toString(sId));
		textField_id.setColumns(10);
		textField_id.setEditable(false);
		JLabel sid = new JLabel("מספר ספק :", JLabel.TRAILING);
		sid.setFont(new Font("Tahoma", Font.BOLD, 12));
		sid.setForeground(new Color(25, 25, 112));
		sid.setLabelFor(textField_id);
		infoPanel.add(textField_id);
		infoPanel.add(sid);
		
		textField_name = new VKTextField(mainMenu);
		textField_name.setHorizontalAlignment(JTextField.RIGHT);
		textField_name.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_name.setColumns(10);		
		JLabel name = new JLabel("שם ספק :", JLabel.TRAILING);
		name.setForeground(new Color(75, 0, 130));
		name.setFont(new Font("Tahoma", Font.BOLD, 12));
		name.setLabelFor(textField_name);	
		infoPanel.add(textField_name);
		infoPanel.add(name);
		
		textField_city = new VKTextField(mainMenu);
		textField_city.setHorizontalAlignment(JTextField.RIGHT);
		textField_city.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_city.setColumns(10);
		JLabel labelCity = new JLabel("עיר (בעברית) :", JLabel.TRAILING);
		labelCity.setForeground(new Color(75, 0, 130));
		labelCity.setFont(new Font("Tahoma", Font.BOLD, 12));
		labelCity.setLabelFor(textField_city);
		infoPanel.add(textField_city);
		infoPanel.add(labelCity);
	   	
		textField_street = new VKTextField(mainMenu);
		textField_street.setHorizontalAlignment(JTextField.RIGHT);
		textField_street.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_street.setColumns(10);
		JLabel label_1 = new JLabel("רחוב (בעברית) :", JLabel.TRAILING);
		label_1.setForeground(new Color(75, 0, 130));
		label_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_1.setLabelFor(textField_street);
		infoPanel.add(textField_street);
		infoPanel.add(label_1);	
	   
		textField_house = new VKTextField(mainMenu);
		textField_house.setHorizontalAlignment(JTextField.RIGHT);
		textField_house.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_house.setColumns(10);
		JLabel label_2 = new JLabel("מס' בית :", JLabel.TRAILING);
		label_2.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_2.setForeground(new Color(75, 0, 130));
		label_2.setLabelFor(textField_house);	
		infoPanel.add(textField_house);
		infoPanel.add(label_2);
		
		textField_zipcode = new VKTextField(mainMenu);
		textField_zipcode.setHorizontalAlignment(JTextField.RIGHT);
		textField_zipcode.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_zipcode.setColumns(10);
		JLabel lblZip = new JLabel("מיקוד :", JLabel.TRAILING);
		lblZip.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblZip.setForeground(new Color(75, 0, 130));
		lblZip.setLabelFor(textField_zipcode);
		infoPanel.add(textField_zipcode);
		infoPanel.add(lblZip);
		
		textField_contactMen = new VKTextField(mainMenu);
		textField_contactMen.setHorizontalAlignment(JTextField.RIGHT);
		textField_contactMen.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_contactMen.setColumns(10);
		JLabel lblContactMen = new JLabel("שם איש קשר :", JLabel.TRAILING);
		lblContactMen.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblContactMen.setForeground(new Color(75, 0, 130));
		lblContactMen.setLabelFor(textField_contactMen);
		infoPanel.add(textField_contactMen);
		infoPanel.add(lblContactMen);

		textField_phoneNo1 = new VKTextField(mainMenu);
		textField_phoneNo1.setHorizontalAlignment(JTextField.RIGHT);
		textField_phoneNo1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_phoneNo1.setColumns(10);
		JLabel label_3 = new JLabel("מס טלפון 1 :", JLabel.TRAILING);
		label_3.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_3.setForeground(new Color(75, 0, 130));
		label_3.setLabelFor(textField_phoneNo1);
		infoPanel.add(textField_phoneNo1);
		infoPanel.add(label_3);
		
		textField_phoneNo2 = new VKTextField(mainMenu);
		textField_phoneNo2.setHorizontalAlignment(JTextField.RIGHT);
		textField_phoneNo2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_phoneNo2.setColumns(10);
		JLabel label_4 = new JLabel("מס טלפון 2 :", JLabel.TRAILING);
		label_4.setForeground(new Color(75, 0, 130));
		label_4.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_4.setLabelFor(textField_phoneNo2);
		infoPanel.add(textField_phoneNo2);
		infoPanel.add(label_4);
		
		textField_phoneCell = new VKTextField(mainMenu);
		textField_phoneCell.setHorizontalAlignment(JTextField.RIGHT);
		textField_phoneCell.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_phoneCell.setColumns(10);
		JLabel label_5 = new JLabel("מס' טלפון סלולרי :", JLabel.TRAILING);
		label_5.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_5.setForeground(new Color(75, 0, 130));
		label_5.setLabelFor(textField_phoneCell);
		infoPanel.add(textField_phoneCell);
		infoPanel.add(label_5);
		
		textField_fax = new VKTextField(mainMenu);
		textField_fax.setHorizontalAlignment(JTextField.RIGHT);
		textField_fax.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_fax.setColumns(10);
		JLabel lblFax = new JLabel("מס' פקס", JLabel.TRAILING);
		lblFax.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFax.setForeground(new Color(75, 0, 130));
		lblFax.setLabelFor(textField_fax);
		infoPanel.add(textField_fax);
		infoPanel.add(lblFax);
	
		textField_email = new VKTextField(mainMenu);
		textField_email.setHorizontalAlignment(JTextField.RIGHT);
		textField_email.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textField_email.setColumns(10);
		JLabel lblEmail = new JLabel("דואר אלקטרוני :", JLabel.TRAILING);
		lblEmail.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblEmail.setForeground(new Color(75, 0, 130));
		lblEmail.setLabelFor(textField_email);
		infoPanel.add(textField_email);
		infoPanel.add(lblEmail);
		
		statusCB=new JComboBox(new ConstantObjects().getSuppStatus());			 
		 JLabel label_status=new JLabel("מצב הספק", JLabel.TRAILING);
		 label_status.setLabelFor(statusCB);
		 infoPanel.add(statusCB);	
		 infoPanel.add(label_status);

		SpringUtilities.makeCompactGrid(infoPanel, 13, 2, //rows, cols
			     400, 5, //initX, initY
			        80, 10); //xPad, yPad
		
	return infoPanel;
	}
	
	
	
	private JPanel getButtonsPanel()
	{
		JPanel buttonsPanel=new JPanel();
	//	buttonsPanel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		buttonsPanel.setBackground(new Color(245, 245,245));
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
		Component verticalStrut_3 = Box.createVerticalStrut(10);
		Component verticalStrut_4 = Box.createVerticalStrut(10);
		
		buttonsPanel.add(get_btnClear());

		
		buttonsPanel.add(verticalStrut_4);
		buttonsPanel.add(get_btnSave());
		buttonsPanel.add(verticalStrut_3);
		buttonsPanel.add(get_btnItems());


		return buttonsPanel;
	}
	
	
	
	
	
	private JPanel get_check_panel()
	{
		check_panel=new JPanel();
	//	check_panel.setBorder(new TitledBorder(null, "\u05D1\u05D3\u05D9\u05E7\u05EA \u05EA\u05E7\u05D9\u05E0\u05D5\u05EA", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		check_panel.setBackground(new Color(245, 245, 245));
        check_panel.setLayout(new BoxLayout(check_panel, BoxLayout.Y_AXIS));
        check_panel.setAlignmentX(CENTER_ALIGNMENT);
		return check_panel;
	}
	
	private JPanel get_check_buttons_panel()
	{
		check_buttons_panel=new JPanel ();
		check_buttons_panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u05D1\u05D3\u05D9\u05E7\u05EA \u05EA\u05E7\u05D9\u05E0\u05D5\u05EA ", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		check_buttons_panel.setLayout(new BoxLayout(check_buttons_panel, BoxLayout.Y_AXIS));
		check_buttons_panel.setBackground(new Color(245, 245, 245));
		check_buttons_panel.add(getButtonsPanel());
		check_buttons_panel.add(Box.createVerticalGlue());
		check_buttons_panel.add(get_check_panel());
		check_buttons_panel.add(Box.createVerticalGlue());
		return check_buttons_panel;
	}
	
	
	
	
	private JButton get_btnClear()
	{
		JButton btnClear=new JButton("ניקוי מסך");
		btnClear.setForeground(new Color(25, 25, 112));
		//btnClear.setBackground(new Color(25, 25, 112));
		btnClear.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		btnClear.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnClear.setAlignmentY(Component.CENTER_ALIGNMENT);
	    Dimension dim = btnClear.getPreferredSize();
	    dim.width = Integer.MAX_VALUE;
	    btnClear.setMaximumSize(dim);
		
		btnClear.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
							
				textField_name.setText(null);
				textField_city.setText(null);
				textField_street.setText(null);
				textField_house.setText(null);
			    textField_zipcode.setText(null);
				textField_phoneNo1.setText(null);	
				textField_phoneNo2.setText(null);
				textField_phoneCell.setText(null);
				textField_email.setText(null);
				textField_fax.setText(null);
				textField_contactMen.setText(null);						
			}
		});
				
		return btnClear;
	}
	
	
	
	private JButton get_btnSave()
	{
		
		JButton btnSave = new JButton("שמור");
		btnSave.setForeground(new Color(25, 25, 112));
		btnSave.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnSave.setBounds(203, 422, 89, 23);
		
		btnSave.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnSave.setAlignmentY(Component.CENTER_ALIGNMENT);
	    Dimension dim = btnSave.getPreferredSize();
	    dim.width = Integer.MAX_VALUE;
	    btnSave.setMaximumSize(dim);
	    
		btnSave.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				JButton savebutt=(JButton) e.getSource();			
				check_panel.removeAll();
				Vector<String> checkingResult=inputCheck();
				Vector<Vector> weekHours=new Vector<Vector>();
				int size=checkingResult.size();                          
				if(size==0)
				{
					JLabel goodLbl=new JLabel("כל הנתונים תקינים");
					goodLbl.setFont(new Font("Tahoma", Font.BOLD, 13));
					goodLbl.setForeground(Color.blue);
					check_panel.add(goodLbl);
					Supplier supp =new Supplier();
					
					  supp.setSuppName(textField_name.getText());
					  supp.setCity(textField_city.getText());
					  supp.setStreet(textField_street.getText());
					  supp.setHouse(textField_house.getText());
					  supp.setSuppZipCode(textField_zipcode.getText());
					  supp.setSuppPhoneNo(textField_phoneNo1.getText());	
					  supp.setSuppPhoneNo2(textField_phoneNo2.getText());
					  supp.setSuppCellNo(textField_phoneCell.getText());
					  supp.setSuppMail(textField_email.getText());
					  supp.setSuppFax(textField_fax.getText());
					  supp.setSuppContactMen(textField_contactMen.getText()) ;
					  supp.setSuppId(textField_id.getText());
					  supp.setStatus((String)statusCB.getSelectedItem());
					  
					 MessageAddNewSupplier msgNewSupp= new MessageAddNewSupplier(supp);
					 
					 try {
							mainMenu.ms.client.sendToServer(msgNewSupp);
							MessageSavingReply result=(MessageSavingReply)mainMenu.ms.client.GetMessage();
							
							if (result.getResult()==1) 
							{
							JLabel goodLbl2=new JLabel("הנתונים נשמרו בהצלחה");
							goodLbl2.setFont(new Font("Tahoma", Font.BOLD, 11));
							goodLbl2.setForeground(Color.blue);
							check_panel.add(goodLbl2);
							
							btnItems.setEnabled(true);
							savebutt.setEnabled(false);
							
							JLabel goodLbl3=new JLabel("ניתן להוסיף מוצרים לספק");
							goodLbl3.setFont(new Font("Tahoma", Font.BOLD, 11));
							goodLbl3.setForeground(Color.blue);
							check_panel.add(goodLbl3);
							}
							else
							{
								JLabel wrongLbl2=new JLabel("בעיית תקשורת.הנתונים לא נשמרו");
								wrongLbl2.setFont(new Font("Tahoma", Font.BOLD, 15));
								wrongLbl2.setForeground(Color.red);
								check_panel.add(wrongLbl2);
							}
							
							
							
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
										
					

					
					
				}
				
				
				else  //wrong input-print all errors on check_panel
				{
					JLabel wrongLbl=new JLabel("תהליך הוספת ספק נכשל :");
					wrongLbl.setFont(new Font("Tahoma", Font.BOLD, 13));
					wrongLbl.setForeground(Color.red);
					check_panel.add(wrongLbl);
					for(int i=0;i<size;i++)
					{
						JLabel error=new JLabel(checkingResult.get(i));
						error.setForeground(Color.red);
						check_panel.add(error);
					}
				}
				
				
				
				mainMenu.getContentPane().validate();
				
		
			}
		});
		
	return btnSave;	
	}
	
	
	private JButton get_btnItems()
	{
		
		btnItems = new JButton("הוספת מוצרים");
		btnItems.setForeground(new Color(25, 25, 112));
		btnItems.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnItems.setBounds(203, 422, 89, 23);
		btnItems.setEnabled(false);                                       
		
		btnItems.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnItems.setAlignmentY(Component.CENTER_ALIGNMENT);
	    Dimension dim = btnItems.getPreferredSize();
	    dim.width = Integer.MAX_VALUE;
	    btnItems.setMaximumSize(dim);
	    
		btnItems.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
						mainMenu.getContentPane().add(new PfAdd_items_to_supplier(mainMenu,textField_id.getText()), BorderLayout.CENTER);	
						mainMenu.getContentPane().validate();
				
			}
		});
		
	return btnItems;	
	}
	
	
	
	 private Vector <String> inputCheck()
	 {
		 Vector<String> result=new Vector<String>();
		 
		 
//=================================================check for name================================
	      
	     String name=textField_name.getText();
	     if(name.length()==0) result.add("יש להקיש שם הספק");
	    
	     
	
	     
//=============================================check for city ======================
	     
	     String city=textField_city.getText();
	     if(city.length()==0) result.add("יש להקיש עיר");
	     else
	     {
	    	 for(int i=0;i<city.length();i++)
	    	 {
	    		 if (city.charAt(i)<'א'|| city.charAt(i)>'ת')
	    		 {
	    			 result.add("עיר אינה תקינה");
	    			 break;
	    		 }	    		 
	    	 }
	     }
	 
	     
//============================================check for telephone======================	 
	     
	     String tel1 = textField_phoneNo1.getText();
	     if (tel1.length() != 0) 
	      {
	    	  if(tel1.length()<9 || tel1.length()>10) result.add("מספר טלפון 1 שגוי");
	          for(int i=0; i<tel1.length();i++)
	        	  if (tel1.charAt(i)<'0'|| tel1.charAt(i)>'9')
		    		 {
		    			 result.add("מספר טלפון  2 שגוי.יש להקיש ספרות בלבד");
		    			 break;
		    		 }	    
		      }
	      else 
	      {
	    	  result.add("יש להקיש מספר טלפון 1");	
	      }
	     
	     
	     String tel2 = textField_phoneNo2.getText();
	     if (tel2.length() != 0) 
	      {
	    	  if(tel2.length()<9 || tel2.length()>10) result.add("מספר טלפון 2 שגוי");
	          for(int i=0; i<tel2.length();i++)
	        	  if (tel2.charAt(i)<'0'|| tel2.charAt(i)>'9')
		    		 {
		    			 result.add("מספר טלפון 2 שגוי.יש להקיש ספרות בלבד");
		    			 break;
		    		 }	    
		      }
	     
	     
	     String tel3 = textField_phoneCell.getText();
	     if (tel3.length() != 0) 
	      {
	    	  if(tel3.length()<9 || tel3.length()>10) result.add("מספר טלפון סלולרי שגוי");
	          for(int i=0; i<tel3.length();i++)
	        	  if (tel3.charAt(i)<'0'|| tel3.charAt(i)>'9')
		    		 {
		    			 result.add("מספר טלפון סלולרי שגוי.יש להקיש ספרות בלבד");
		    			 break;
		    		 }	    
		      }
	     

//=============================================check for fax ==================================
	     
	     String faxNo = textField_fax.getText();
	     if (faxNo.length() != 0) 
	      {
	    	  if(faxNo.length()<9 || faxNo.length()>10) result.add("מספר פקס שגוי");
	          for(int i=0; i<faxNo.length();i++)
	        	  if (faxNo.charAt(i)<'0'|| faxNo.charAt(i)>'9')
		    		 {
		    			 result.add("מספר פקס שגוי.יש להקיש ספרות בלבד");
		    			 break;
		    		 }	    
		      }
	     
	     
//=============================================check for ZIP ==================================
	     
	     String zip = textField_zipcode.getText();
	     if (zip.length() != 0) 
	      {
	    	  
	          for(int i=0; i<zip.length();i++)
	        	  if (zip.charAt(i)<'0'|| zip.charAt(i)>'9')
		    		 {
		    			 result.add("מיקוד שגוי.יש להקיש ספרות בלבד");
		    			 break;
		    		 }	    
		      }
			        
	     
		 return result;
		 
	 }
	
	
}